<?php

// this example.php file was last update May 28th 2009, 10:55am NZST

require_once('../class/analytics_api.php');

// enter your login, password and id into the variables below to try it out

$login = 'ledanglong@gmail.com';
$password = 'khongphaiemdau';

$api = new analytics_api();

if($api->login($login, $password)) {
        
        $api->load_accounts();
	
	$tableId = $api->getAccountsTableIdFromName('adslvnpt.com');
	
	$data = $api->data($tableId, 'ga:date', 'ga:visits,ga:pageviews','ga:date',date('Y-m-d', strtotime('1 month ago')),date('Y-m-d'),33);
	$file_data = "lib/fusion-charts/data/daily_visits.xml";
	$content = '<graph caption="Daily Visits" subcaption="(from '.date('d/m/y', strtotime('1 month ago')).' to '.date('d/m/Y').')" lineThickness="1" showValues="0" formatNumberScale="0" anchorRadius="2"';
	$content .='divLineAlpha="20" divLineColor="CC3300" showAlternateHGridColor="1"';
	$content .='alternateHGridColor="CC3300" shadowAlpha="40" numvdivlines="30"';
	$content .='chartRightMargin="35" bgColor="ffffff" alternateHGridAlpha="5" limitsDecimalPrecision="0" divLineDecimalPrecision="0" decimalPrecision="0">';
	foreach($data as $i => $value ) {
	    $date[] = substr($i,6,2);
		$visit[] = $value['ga:visits'];
		$pageview[] = $value["ga:pageviews"];
	}
	// date
	$content .= '<categories>';
	for ($i=0;$i<count($date);$i++) {
		$content .= '<category name="'.$date[$i].'" />';
	}
	$content .= '</categories>';
	// visit
	$content .= '<dataset seriesName="Visit" color="1D8BD1" anchorBorderColor="1D8BD1" anchorBgColor="1D8BD1">';
	for ($i=0;$i<count($visit);$i++) {
		$content .= '<set value="'.$visit[$i].'" />';
	}
	$content .= '</dataset>';
	// Page view
	$content .= '<dataset seriesName="Pageview" color="F1683C" anchorBorderColor="F1683C" anchorBgColor="F1683C">';
	for ($i=0;$i<count($pageview);$i++) {
		$content .= '<set value="'.$pageview[$i].'" />';
	}
	$content .= '</dataset>';
	$content .= '</graph>';
	file_put_contents($file_data,$content);
}
?>
